package com.wk.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wk.common.bean.PageVo;
import com.wk.common.bean.Query;
import com.wk.common.bean.QueryCondition;
import com.wk.pms.entity.Attr;
import com.wk.pms.entity.AttrAttrgroupRelation;
import com.wk.pms.entity.AttrGroup;
import com.wk.pms.entity.ProductAttrValue;
import com.wk.pms.mapper.AttrAttrgroupRelationMapper;
import com.wk.pms.mapper.AttrGroupMapper;
import com.wk.pms.mapper.AttrMapper;
import com.wk.pms.mapper.ProductAttrValueMapper;
import com.wk.pms.service.AttrGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.pms.vo.AttrGroupVO;
import com.wk.pms.vo.ItemGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup> implements AttrGroupService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private AttrAttrgroupRelationMapper relationMapper;

    @Autowired
    private ProductAttrValueMapper attrValueMapper;

    @Override
    public PageVo queryGroupByPage(QueryCondition condition, Long catId) {
        IPage<AttrGroup> page = baseMapper.selectPage(
                new Query<AttrGroup>().getPage(condition),
                new QueryWrapper<AttrGroup>().eq(catId!=null,"catelog_id", catId));
        return new PageVo(page);
    }

    @Override
    public AttrGroupVO queryGroupWithAttrsByGroupId(Long groupId) {
        AttrGroupVO attrGroupVo = new AttrGroupVO();

        //查询AttrGroup对象
        AttrGroup attrGroup = baseMapper.selectById(groupId);
        //赋值给vo对象
        BeanUtils.copyProperties(attrGroup, attrGroupVo);

        //根据gid查询关联关系，并获取Attrs
        List<AttrAttrgroupRelation> relationList = relationMapper.selectList(new QueryWrapper<AttrAttrgroupRelation>().eq("attr_group_id", groupId));
        if (CollectionUtils.isNotEmpty(relationList)) {
            attrGroupVo.setRelations(relationList);
        }

        //根据AttrsIds查询所有规格参数
        List<Long> attrIds = relationList.stream().map(AttrAttrgroupRelation::getAttrId).collect(Collectors.toList());
        List<Attr> attrs = attrMapper.selectBatchIds(attrIds);
        attrGroupVo.setAttrEntities(attrs);

        return attrGroupVo;
    }

    /**
     * 根据categoryId查询三级分类下的所有属性分组，根据分组中的ID查询中间表,再根据中间表的attrids查询参数信息
     */
    @Override
    public List<AttrGroupVO> queryGroupWithAttrsByCategoryId(Long categoryId) {

        //根据categoryId查询三级分类下的所有属性分组
        List<AttrGroup> groups = baseMapper.selectList(new QueryWrapper<AttrGroup>().eq(categoryId != null, "catelog_id", categoryId));

        //根据分组中的ID查询中间表,再根据中间表的attrids查询参数信息
        return groups.stream().map(group->queryGroupWithAttrsByGroupId(group.getAttrGroupId())).collect(Collectors.toList());
    }

    /**
     * 根据spuId和分类ID查询spu对应的所有分类信息及值
     *  根据CategoryId查询所有的attr_group_id
     *  根据attr_group_id找到attrIds
     *  根据attrIds和spuId找到属性值
     */
    @Override
    public List<ItemGroupVO> queryItemGroupByCategoryIdAndSpuId(Long categoryId, Long spuId) {
        //根据三级分类id查询对应的属性分组
        List<AttrGroup> attrGroups = baseMapper.selectList(new QueryWrapper<AttrGroup>().eq("catelog_id", categoryId));

        return attrGroups.stream().map(group->{

            ItemGroupVO groupVO = new ItemGroupVO();
            //属性名称
            groupVO.setName(group.getAttrGroupName());

            //根据分组ID获取中间表
            List<AttrAttrgroupRelation> relations = relationMapper.selectList(new QueryWrapper<AttrAttrgroupRelation>().eq("attr_group_id", group.getAttrGroupId()));

            //取出中间表中对应的属性ID
            List<Long> attrIds = relations.stream().map(relation -> relation.getAttrId()).collect(Collectors.toList());

            //根据属性ID获取属性值
            List<ProductAttrValue> attrValueList = attrValueMapper.selectList(new QueryWrapper<ProductAttrValue>().eq("spu_id", spuId).in("attr_id", attrIds));

            groupVO.setBaseAttr(attrValueList);

            return groupVO;

        }).collect(Collectors.toList());
    }
}
